var app = getApp();
var net = require("../../utils/net.js");
var utils = require("../../utils/util.js");
var posting = false;
var dialog = null;
Page({
  data: {
    hidden: true,
    tipmsg: "",
    tipno: 0,
    mobile: "",
    old_psw: "",
    new_psw: "",
    pswnewconfirm: "",
    disable: false,
    mobiletip: 0
  },

  input: function (e) {
    var old_psw = this.data.pswnewconfirm;
    var value = e.detail.value + "";
    var key = e.currentTarget.id + "";
    if (key == "mobile") {
      if (value.length == 11)
        if (value.length >= 6)
          this.inputtip(2, "mobiletip");
        else
          this.inputtip(0, "mobiletip");
    } else if (key == "pswold") {
      if (value.length >= 6)
        this.inputtip(2, "pswoldtip");
      else
        this.inputtip(0, "pswoldtip");
    } else if (key == "pswnew") {
      if (value.length >= 6)
        this.inputtip(2, "pswnewtip");
      else
        this.inputtip(0, "pswnewtip");
    } else if (key == "pswnewconfirm") {
      if (value.length >= 6 && value == this.data.pswnew)
        this.inputtip(2, "pswnewconfirmtip");
      else
        this.inputtip(0, "pswnewconfirmtip");
    }
    this.setData({
      [key]: value,
    });
    if (this.data.mobile.length < 11) {
      this.showtip(5, "请输入正确的手机号");
      return;
    }
    else if (this.data.new_psw.length == "") {
      this.showtip(5, "未输入密码");
    }
    else  if (this.data.mobile.length =="") {
      this.showtip(5, "未输入手机号");
      return;
    }
    
    else if (this.data.old_psw == this.data.new_psw) {
      this.showtip(5, "密码已经一致了");
    }
    else if (this.data.new_psw.length == "") {
      this.showtip(5, "请输入新密码");
      return;
    }
    else if (this.data.mobile.length > 1) {
      this.showtip(5, "密码已经一致了");
    }

  },

  inputtip(val, key) {
    this.setData({
      [key]: val
    })
  },
  showtip(lv, msg) {
    this.setData({
      tipno: lv,
      tipmsg: msg
    })
  },
  Confirm: function () {
    this.setData({
      hidden: true
    })
  },

  reset: function () {
    var that = this;
    var old_psw = this.data.pswnewconfirm;
    var new_psw = this.data.new_psw;
    if (this.data.mobile.length < 11) {
      this.showtip(1, "请输入正确的手机号码");
      return;
    }
    else if (this.data.new_psw.length =="") {
      this.showtip(1, "请输入新密");
      return;
    }
    if (old_psw != new_psw) {
      this.showtip(1, "密码必须一致");
      return false;
    } else if (old_psw == new_psw) {
      var that = this;
      net.request({
        url: '/ModifyPsw',
        data: {
          mobile: that.data.mobile,
          old_psw: that.data.old_psw,
          new_psw: that.data.new_psw

        },
        success: function (res) {
          console.log(res.data)
          if (res.data.flag) {
            wx.navigateTo({
              url: "../index/index",
              success: function (res) { },
              fail: function (res) { },
              complete: function (res) { },
            })
          } else {
            dialog.showDialog({
              msg: "账号或密码错误"
            });
          }
        }
      })
    }
  },
  onLoad: function () {

  },
  onReady: function () {
    dialog = this.selectComponent("#dialog");
  },

})

